<template>
  <div>
    <el-aside width="240px" class="aside_rolelist">
      <div class="asidelist">
        <div class="hd">
          <div>角色列表</div>
          <div>
            <el-button
              class="header-icon el-icon-plus"
              type="primary"
              plain
              @click="addGradeVisible=true"
            >添加</el-button>
          </div>
        </div>
        <div class="role-container" id="rolelist">
          <el-tree
            :data="data_rolelist"
            show-checkbox
            node-key="id"
            :highlight-current="true"
            @node-click="nodebg"
            :expand-on-click-node="false"
            :props="defaultProps"
          >
            <span class="custom-tree-node" slot-scope="{ node, data }">
              <span class="treefont">{{ node.label }}</span>
              <span class="bticon">
                <el-dropdown>
                    <i class="el-icon-guide"></i>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>
                       <span style="color:#409EFF" class="el-icon-edit" @click="() => edit(data)">编辑</span>
                    </el-dropdown-item>
                    <el-dropdown-item>
                       <span style="color:#F56C6C" class="el-icon-delete" @click="() => remove(node, data)">删除</span>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </span>
            </span>
          </el-tree>
        </div>
      </div>
      <!-- 新增角色-->
      <el-dialog title="新增" :visible.sync="addGradeVisible" width="30%">
        <el-form ref="formData" :model="formData" label-width="70px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addGradeVisible = false">取 消</el-button>
          <el-button
            type="primary"
            :disabled="!formData.name"
            @click="addsaveClick(formData)"
          >确 定</el-button>
        </span>
      </el-dialog>
      <!-- 编辑角色 -->
      <el-dialog title="修改" :visible.sync="editGradeVisible" width="30%">
        <el-form ref="formData" :model="formData" label-width="70px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editGradeVisible = false">取 消</el-button>
          <el-button
            type="primary"
            :disabled="!formData.name"
            @click="saveeditClick(formData)"
          >确 定</el-button>
        </span>
      </el-dialog>
    </el-aside>
  </div>
</template>
<script>
import bus from "../../common/bus";
export default {
  data() {
    return {
      data_rolelist: [],
      defaultProps: {
        label: "name"
      },
      // 控制添加弹窗
      addGradeVisible: false,
      //控制编辑弹窗
      editGradeVisible: false,
      formData: {
        "name": "",
        "projectId": localStorage.getItem("project_Id"),
        "projectTypeId": 0,
        "put":0,
        "edit":0,
        "remove":0
      }
    };
  },
  methods: {
    nodebg(val) {
      let v=val;
      bus.$emit("putrole", v);
    },
    //添加角色
    addsaveClick(data) {
      this.$axios.post("/ProjectRole/", this.formData)
        .then(res => {
          if (!res.data.hasError) {
            this.$message({
              message: "角色添加成功",
              type: "success"
            });
            this.getrolelist();
            this.addGradeVisible = false;
          }else{
            this.$message({
              message: res.data.message,
              type: "error"
            });
          }
        });
    },
    //角色编辑弹窗
    edit(data) {
      this.formData = data;
      this.editGradeVisible = true;
    },
    //提交编辑
    saveeditClick(data) {
      this.$axios
        .put(`ProjectRole/${data.projectRoleId}`, data)
        .then(res => {
          if (res.status == 200) {
            this.$message({
              message: "角色修改成功",
              type: "success"
            });
            this.getrolelist();
            this.editGradeVisible = false;
          }
        });
    },
    remove(node, data) {
      this.$confirm("是否确认删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
        // type: 'warning'
      })
        .then(() => {
          this.$axios
            .delete(`/ProjectRole/${data.projectRoleId}`)
            .then(res => {
              if (res.data.hasError == false) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                //刷新
                this.getrolelist();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //获取所有角色
    getrolelist() {
      this.$axios.get("/ProjectRole/Project/"+this.formData.projectId)
        .then(res => {
          this.data_rolelist = res.data.data;
        });
    }
  },
  created() { 
    this.getrolelist();
  }
};
</script>
<style lang="scss" scope>
.asidelist {
  background-color: #fff;
  border-radius: 5px;
  width: 100%;
  height: 100%;
  .hd {
    height: 60px;
    border-bottom: 1px solid #ebeef5;
    box-sizing: border-box;
    padding: 0 10px;
    line-height: 60px;
    display: flex;
    justify-content: space-between;
  }
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 18px;
  }
  .el-checkbox__inner {
    width: 0;
  }
  .el-tree-node__content {
    height: 40px;
  }
 
}
.aside_rolelist {
  height: 100%;
}
</style>